package com.quick.driver.controller;

import com.quick.common.Constent.Quick;
import com.quick.common.bean.Driver;
import com.quick.common.dto.*;
import com.quick.common.service.IDriverService;
import com.quick.common.util.ImageUtil;
import com.quick.common.util.Result;
import com.quick.common.util.TokenUtil;
import org.antlr.runtime.Token;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

@RestController
@RequestMapping("/driver")
public class DriverController {
    @DubboReference
    private IDriverService driverService;

    @PostMapping("login")
    public Result<String> driverLogin(@RequestBody LoginDTO loginDTO){
        return Result.success("登录成功",driverService.driverLogin(loginDTO));
    }

    @GetMapping("getTokenInfo")
    public Result<TokenInfoDTO> getTokenInfo(@RequestHeader("Authorization") String authorizationHeader){
        String token;
        if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
            token = authorizationHeader.substring(7);
        } else {
            token = authorizationHeader;
        }
        String s = TokenUtil.getInfo(token, Quick.TOKEN_KEY_ID);
        System.out.println("ID"+s);
        int id = Integer.parseInt(s);
        String role = TokenUtil.getInfo(token, Quick.TOKEN_KEY_ROLE);
        System.out.println("ROLE"+role);
        //String name = TokenUtil.getInfo(token, Quick.TOKEN_KEY_NAME);
        TokenInfoDTO tokenInfo = new TokenInfoDTO();
        tokenInfo.setId(id);
        tokenInfo.setRole(role);
        System.out.println("token"+tokenInfo);
        return Result.success("",tokenInfo);
    }

    @PostMapping("register")
    public Result<String> driverRegister(@RequestBody RegisterDTO registerDTO){
        driverService.driverRegister(registerDTO);
        return Result.success("司机注册成功");
    }

    @GetMapping("getDriverById/{id}")
    public Result<Driver> getDriverById(@PathVariable("id") Integer id){
        System.out.println("司机信息"+driverService.getDriverById(id));
        return Result.success("查询成功",driverService.getDriverById(id));
    }
    @PostMapping("/file")
    public Result upload(@RequestParam("file") MultipartFile file){
        try {
            InputStream is=file.getInputStream();
            String filename=file.getOriginalFilename();
            String newFileName = ImageUtil.upload(is,filename);
            return Result.success("图片上传成功",newFileName);
        } catch (IOException e) {
            throw new RuntimeException("图片上传失败");
        }
    }
    @PostMapping("upload")
    public Result<String> editImg(@RequestBody ImageDTO imageDTO){
        String img = driverService.upload(imageDTO);
        System.out.println("img"+img);
        return Result.success(img);
    }
    @PutMapping("editDriver")
    public Result<Integer> editDriver(@RequestBody Driver driver){
        return Result.success("修改成功",driverService.updateDriver(driver));
    }
    @PutMapping("changePassword")
    public Result changePassword(@RequestBody PasswordDTO passwordDTO){
        try{
            if (!Objects.equals(passwordDTO.getNewPassword(),passwordDTO.getConfirmPassword())){
                return Result.error("两次密码不一致");
            }
            System.out.println("passwordDTO"+passwordDTO);
            driverService.changePassword(passwordDTO);
            return Result.success("修改成功");
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }
}
